/**
 * 
 */
package com.effectiv.gooruda.rest.service.impl;

import java.io.ByteArrayInputStream;

import javax.ws.rs.core.Response;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.effectiv.gooruda.rest.docs.annotation.ApiResponseObject;

import com.effectiv.gooruda.rest.domain.FileStorage;
import com.effectiv.gooruda.rest.domain.User;
import com.effectiv.gooruda.rest.service.api.FileStorageService;
import com.effectiv.gooruda.rest.service.api.UserService;
import com.mongodb.BasicDBObject;
import com.mongodb.gridfs.GridFSFile;

/**
 * @author Dhrubo
 * 
 */

@Component
@Slf4j
public class FileStorageServiceImpl extends FileStorageService {

	private UserService userService;

	public Response store(FileStorage storage) {
		log.info("storage == {}", storage);

		User owner = null;

		if (StringUtil.isNotBlank(storage.getOwner())) {

			owner = userService.findByUserNameInternal(storage.getOwner());
		}

		String appFileName = getAppFileName(storage.getFileName(),
				storage.getFolder());

		FileStorage eStorage = null;

		Query query = new Query();
		query.addCriteria(Criteria.where("appFileName").is(appFileName));

		eStorage = getMongoTemplate().findOne(query, FileStorage.class,
				getCollection());

		if (eStorage != null) {
			throwException(600, "A file with name - " + storage.getFileName()
					+ ", already exists in folder - " + storage.getFolder()
					+ ". You may consider using update operation instead.");
		}
		
		//set properties
		byte content [] = storage.getFileContent();
		
		
		if(content == null || content.length == 0) {
			throwException(601, "Empty file cannot be stored.");
		}
		
		storage.setSize(content.length);
		//DONOT store file content in db rather in gridfs
		storage.setFileContent(null);
		
		try {

			BasicDBObject metadata = new BasicDBObject();
			metadata.put("version", 0);
			
			 GridFSFile gridFile = getGridFsTemplate().store(new ByteArrayInputStream(content),
			 appFileName, storage.getType(), metadata);
			 storage.setGridFileId(gridFile.getId().toString());
			
			getMongoTemplate().save(storage, getCollection());
		} catch (Exception e) {
			throwException(602, "Error saving to GRID File store - " + e.getLocalizedMessage());
		}

		return null;
	}

	@Override
	public @ApiResponseObject
	Response update(FileStorage storage) {
		log.info("storage == {}", storage);
		
		return null;
	}

	@Override
	public @ApiResponseObject
	Response addTags(String fileName, String folder, String tags) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response removeTags(String fileName, String folder, String tags) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response removeAllTags(String fileName, String folder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response removeOwner(String fileName, String folder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response changeOwner(String fileName, String folder, String oldOwner,
			String newOwner) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response moveFolder(String fileName, String oldFolder, String newFolder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response deleteFolder(String folder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response renameFolder(String oldFolder, String newFolder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response deleteByUser(String userName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response deleteByName(String fileName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response deleteAllByUser(String userName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response deleteAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response countByType(String type) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response countByUser(String userName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response count() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response findByType(String type, Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response findByOwner(String userName, Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response findAll(Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response findByTags(String[] tags, Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response download(String fileName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public @ApiResponseObject
	Response findByName(String fileName, Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

}
